package com.example.demo.controller;

import com.example.demo.entity.Company;
import com.example.demo.result.RestBean;
import com.example.demo.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @PostMapping("/create")
    public RestBean<Company> createCompany(@RequestBody Company company) {

        int result = companyService.createCompany(company);

        if (result == 1) {
            System.out.println("添加成功！");
            return RestBean.success(company);
        }
        System.out.println("添加失败！");
        return RestBean.failure(201,null);
    }



    @PostMapping("/delete")
    public RestBean<Company> deleteCompany(@RequestBody Company company) {

        int result = companyService.deleteCompany(company.getCid());

        if (result == 1) {
            return RestBean.success(company);
        }
        return RestBean.failure(404,null);
    }

    @DeleteMapping("/updelete/{Cid}")
    public RestBean<String> deleteCompany(@PathVariable Integer Cid) {

        companyService.updateIsdeleted(Cid);
        return RestBean.success("公司删除成功！");
    }

    @PostMapping("/update")
    public RestBean<Company> updateCompany(@RequestBody Company company) {

        int result = companyService.updateCompany(company);

        if (result == 1) {
            System.out.println("修改成功！");
            return RestBean.success(company);
        }
        System.out.println("修改失败！");
        return RestBean.failure(201,null);
    }

    @GetMapping("/findall")
    public RestBean<List<Company>> findAllcompanies() {

        List<Company> companies = companyService.findAllcompanies();

        if (!companies.isEmpty()) {
            System.out.println("查询成功！");
            return RestBean.success(companies);
        } else {
            System.out.println("查询失败！");
            return RestBean.failure(404, null);
        }
    }

    @GetMapping ("/findone")
    public RestBean<Company> findcompany(@RequestBody Company company) {

        /*Integer isDeleted = company.getIsdeleted();

        if (isDeleted != null && isDeleted == 0) {
            System.out.println("查询失败！");
            return RestBean.failure(404, null);
        }*/

        Company company1 = companyService.findCompany(company.getCid());

        if (company1 == null) {
            System.out.println("查询失败！");
            return RestBean.failure(404, null);
        } else {
            System.out.println("查询成功！");
            return RestBean.success(company1);
        }

    }
}